<template>
  <el-dialog title="采购订单详情" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="采购类型" prop="ctgId">
            <el-input v-model="dataForm.ctgName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="制造商" prop="supplyName">
            <el-input v-model="dataForm.supplyName" readonly disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="调拨申请单号" prop="linkBillId">
            <el-input v-model="dataForm.linkBillId" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" :rows="2" v-model="dataForm.memo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" header-cell-class-name="miui-header-cell-style" @selection-change="handleSelectionChange">
      <el-table-column prop="barcode" label="商品代码"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="spec" label="型号"> </el-table-column>
      <el-table-column prop="applyQuantity" label="采购数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.applyQuantity" disabled></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="备注" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-input v-model="scope.row.memo" disabled></el-input>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
    import {receiveApplyGetObj, receiveApplyAddObj, receiveApplyPutObj} from '@/api/eims/purchase/receive-apply'
    import { fetchCtgList } from '@/api/eims/ctg'
    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          applyStatus: '2',
          ctgId: '',
          ctgName: '',
          supplyId: '',
          supplyName: '',
          memo: '',
          detailList: []
        },
        ctgOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {ctgId: [{ required: true,message: '请选择采购类别'}],supplyName: [{required:true,message: '请选择制造商'}]},
        supplySearchVisible: false,
        goodsListVisible: false,
        selectionList: []
      }
    },
    methods: {
      init (id) {
        this.getCtgList()
        this.dataForm.id = id || 0
        this.dataForm.supplyId = ''
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            receiveApplyGetObj(this.dataForm.id).then(result => {
              if(response.data.code == '0'){
                this.dataForm = result.data.data.data
                this.dataList = result.data.data.data.detailList
                this.dataList.forEach(item => {
                  item.barcode = item.goodsCode
                  item.name = item.goodsName
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 商品类别改变时获取名字
      ctgSelectChange ($event) {
        this.ctgOptions.forEach(item => {
          if(item.value == $event){ this.dataForm.ctgName = item.name }
        })
      },
      // 打开制造商列表弹窗
      checkSupplyList () {
        this.supplySearchVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init(this.dataForm.supplyId)
        })
      },
      // 选择制造商
      renewFormData (data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        // 判断是否选中采购类型
        if(!this.dataForm.ctgId){
          this.$message.warning('请选择采购类型')
          return
        }
        // 判断是否选中制造商
        if(!this.dataForm.supplyId){
          this.$message.warning('请选择制造商')
          return
        }
        let params = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
        let data = {params: params,data: this.dataList}
        this.goodsListVisible = true
        this.$nextTick(() => {  this.$refs.sinleGoodsList.init(data) })
      },
      handleSelectionChange(val) {
        this.selectionList = val
      },
      // 删除选中的商品
      delTableData() {
        let data = this.selectionList
        data.forEach(item => { this.dataList.splice(item,1) })
      },
      //更新表单数据
      updateFormData(data) {
        this.dataList = data
        data.forEach(item => {
          let list = []
          list.push({goodsId: item.id})
          this.dataForm.detailList = list
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailList.forEach((item,index) => {
              item.applyQuantity = this.dataList[index].applyQuantity
            })
            receiveApplyAddObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
               (this.dataForm.applyStatus == '1') ? this.$message.success('新增并审核成功') : this.$message.success('新增成功')
               this.visible = false
               this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },

      /*
      * *  获取采购类型列表
      */
     getCtgList(){
       fetchCtgList().then(response => {
         if(response.data.code == '0'){
          let dataList = response.data.data.records
          let list = []
          dataList.forEach(item => {
            list.push({value:item.id,name:item.name})
          })
          this.ctgOptions = list
         }else{
           this.$message.error(response.data.msg)
         }
       })
     }
    }
  }
</script>
